<template>
  <div>
    <!-- 头部 -->
    <Header @changePage="changePage" />
    <!-- 内容 -->
    <Suspense>
      <template #default>
        <transition name="fade">
          <KeepAlive>
            <component :is="AsyncComponent" />
          </KeepAlive>
        </transition>
      </template>
      <template #fallback>
        <div class="loading">页面加载中...</div>
      </template>
    </Suspense>
    <!-- 底部版权 -->
    <Footer />
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
import { ref, defineAsyncComponent, computed } from 'vue'
import Header from '../components/Header.vue'
import Footer from '../components/Footer.vue'

const router = useRouter();
// 切换
const currentPage = ref('discover')

const componentsMap: Record<string, any> = {
  discover: defineAsyncComponent(() => import('./Discover.vue')),
  b: defineAsyncComponent(() => import('./MyMusic.vue')),
  myinfo: defineAsyncComponent(() => import('./MyInfo.vue')),
  creationcenter: defineAsyncComponent(() => import('./CreationCenter.vue')),
  attention: defineAsyncComponent(() => import('./Attention.vue')),
  download: defineAsyncComponent(() => import('./Download.vue')),
  infoupdate: defineAsyncComponent(() => import('../views/InfoUpdate.vue'))
}

const AsyncComponent = computed(() => componentsMap[currentPage.value])

const changePage = (page: string, path: string) => {
  if (path != '') {
    router.push(path)
    console.log("ddddddddddd")
    return
  }
  console.log(page, "ssssssssss")
  currentPage.value = page
}
</script>

<style lang="scss" scoped>
.loading {
  text-align: center;
  margin-top: 20px;
  font-size: 18px;
  color: #999;
}

/* 切换动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>